﻿using System;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using WiMo.Games.Drawables;
using WiMo.Games.Drawing.Silverlight;
using System.Windows;

namespace WiMo.Games.Drawing.WP7S.Drawables
{
    public class Polygon : SpriteShapeBase
    {
        System.Windows.Shapes.Polygon _polygon;

        public Polygon()
        {
            ShapeType = WiMo.Games.Drawables.ShapeType.Polygon;
            Origin = WiMo.Games.Drawables.Origin.TopLeft;
        }

        public override void Render(IDisplay display)
        {
            _polygon = new System.Windows.Shapes.Polygon();

            if (this.BorderColor != Color.Transparent)
            {
                _polygon.Stroke = new System.Windows.Media.SolidColorBrush(this.BorderColor.GetMediaColor());
                _polygon.StrokeThickness = this.BorderWidth;
            }

            if (this.FillColor != Color.Transparent)
                _polygon.Fill = new System.Windows.Media.SolidColorBrush(FillColor.GetMediaColor());                        
            
            foreach(var point in Points)
                _polygon.Points.Add(new Point(point.X, point.Y));

            if (Origin == WiMo.Games.Drawables.Origin.Center)
            {
                _polygon.SetValue(Canvas.TopProperty, (double)Position.Y - Size.Height / 2);
                _polygon.SetValue(Canvas.LeftProperty, (double)Position.X - Size.Width / 2);
            }
            else
            {
                _polygon.SetValue(Canvas.TopProperty, (double)Position.Y);
                _polygon.SetValue(Canvas.LeftProperty, (double)Position.X);
            }

            DrawInformation = _polygon;

            var element = DrawInformation as UIElement;

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scale = new ScaleTransform();

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scale);

            element.RenderTransform = tg;

            display.Render(this);
        }
    }
}
